package tasks;


import Main.Parameters;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Scanning folder with tasks for task file with maximum priority and/or task file with closest time start parametr
 * @author zem
 */
public class getNextTask {

    /**
     * @param NextTaskAdress next task adress; $null$ -if no tasks
     */
   public getNextTask() {

        //setting  parameters
        Parameters.CurrentTaskNumber = -1;
        int nexttaskpriority = 0;
        String task_start_time = "null";
        String nexttaskstarttime = null;
        int task_priority = Parameters.DefaultTaskPriority;
        boolean firsttaskscanned = false;

        //scaning tasks

        for (int i = 0; i < Parameters.TotalTasks; i++) {
            boolean alreadydone = false;
            if(Parameters.DeleteTaskWhenDone==0){ //checking if tasks are not deleted when done and not moved
            if(Parameters.MoveDoneTask==0 | Parameters.MoveFailedTask==0){

                for (int j = 0; j < Parameters.TasksDone; j++) {
                    if (Parameters.AdrOfDoneTasks[j].equals(Parameters.TaskName[i])) {
                        alreadydone = true;
                        j = Parameters.TasksDone;
                    }
                }


            }
            }
            
            if (!alreadydone) {
                try {
                    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                    Document doc = docBuilder.parse(new File(Parameters.TaskFolder+Parameters.TaskName[i]));
                    doc.getDocumentElement().normalize();

                    NodeList nodelist = doc.getElementsByTagName("run_at");
                    int length = nodelist.getLength();
                    if (length < 1) {//if time to start is not specified

                        task_start_time = Parameters.CurrentSystemTime;
                    } else { // getting start time
                        Element nelement = (Element) nodelist.item(0);

                        NodeList current = nelement.getChildNodes();
                        String temp = ((Node) current.item(0)).getNodeValue().trim();
                        temp = "" + temp.charAt(0) + temp.charAt(1) + temp.charAt(3) + temp.charAt(4) + temp.charAt(6) + temp.charAt(7);
                        int temp1 = Integer.parseInt("" + Parameters.CurrentSystemTime.charAt(8) + Parameters.CurrentSystemTime.charAt(9) + Parameters.CurrentSystemTime.charAt(10) + Parameters.CurrentSystemTime.charAt(11) + Parameters.CurrentSystemTime.charAt(12) + Parameters.CurrentSystemTime.charAt(13));
                        int temp2 = Integer.parseInt(temp);
                        int year = Integer.parseInt("" + Parameters.CurrentSystemTime.charAt(0) + Parameters.CurrentSystemTime.charAt(1) + Parameters.CurrentSystemTime.charAt(2) + Parameters.CurrentSystemTime.charAt(3));
                        int month = Integer.parseInt("" + Parameters.CurrentSystemTime.charAt(4) + Parameters.CurrentSystemTime.charAt(5));
                        int day = Integer.parseInt("" + Parameters.CurrentSystemTime.charAt(6) + Parameters.CurrentSystemTime.charAt(7));
                        if (temp2 < temp1) {

                            if (day == 31) {
                                if (month == 12) {
                                    year++;
                                    month = 01;
                                    day = 01;
                                } else {
                                    month++;
                                }
                            } else if (day == 30) {
                                if (month == 4 | month == 6 | month == 9 | month == 11) {
                                    month++;
                                    day = 1;
                                } else {
                                    day++;
                                }
                            } else if (day == 29 & month == 2) {
                                month++;
                                day = 1;
                            } else if (day == 28 & month == 2) {
                                if (year % 4 == 0) {
                                    day++;
                                } else {
                                    day = 01;
                                    month++;
                                }

                            }
                        }
                        String str = "" + year;
                        if (month < 10) {
                            str += "0";
                        }
                        str += month;
                        if (day < 10) {
                            str += "0";
                        }
                        str += day;
                        temp = str + temp;
                        task_start_time = temp;

                    }
                    //getting priority of task
                    nodelist = doc.getElementsByTagName("priority");
                    length = nodelist.getLength();
                    if (length < 1) {//if priority is not specified
                        task_priority = Parameters.DefaultTaskPriority;

                    } else {
                        Element nelement = (Element) nodelist.item(0);

                        NodeList current = nelement.getChildNodes();
                        task_priority = Integer.parseInt(((Node) current.item(0)).getNodeValue().trim());

                    }
                    if (firsttaskscanned) {
                        //comparing results
                            double time_st = Double.parseDouble(task_start_time); //scanned
                            double time_nt = Double.parseDouble(nexttaskstarttime); //next task time
                            double cur_time = Double.parseDouble(Parameters.CurrentSystemTime);
                        if (Parameters.OldTaskPriorirty == 0) {
                            
                            if ((time_st<time_nt) | (time_st == time_nt & task_priority > nexttaskpriority)) {
                                
                                    nexttaskpriority = task_priority;
                                    nexttaskstarttime = task_start_time;
                                    Parameters.CurrentTaskNumber = i;

                            }

                        } else if (Parameters.OldTaskPriorirty == 1) {
                             if(time_st<time_nt & time_nt>cur_time){

                                    nexttaskpriority = task_priority;
                                    nexttaskstarttime = task_start_time;
                                    Parameters.CurrentTaskNumber = i;
                             }else
                              if(time_st==time_nt | (time_st<=cur_time & time_nt<=cur_time)){
                                  if(task_priority>nexttaskpriority){
                                   nexttaskpriority = task_priority;
                                    nexttaskstarttime = task_start_time;
                                    Parameters.CurrentTaskNumber = i;
                                  }

                              }

                        }


                    } else {
                        nexttaskpriority = task_priority;
                        nexttaskstarttime = task_start_time;
                        firsttaskscanned = true;
                        Parameters.CurrentTaskNumber = i;
                    }

                //comparing times and priority

                } catch (Exception er) {
                    System.err.println(er + " getNextTask");
                }

            }

        }

    }
}
